package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompDevBindInfoMapper;
import cc.rengu.igas.bomp.common.entity.BspsDevBindInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @date 2019/08/15
 */
public class BompDevBindInfoMapperImpl implements BompDevBindInfoMapper {

    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public List<BspsDevBindInfo> selectBompDevBindInfoListByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompDevBindInfoListByMchntNo");
        BspsDevBindInfo devBindInfo = new BspsDevBindInfo();
        devBindInfo.setInstId(instId);
        devBindInfo.setBindMchntNo(mchntNo);

        List<Object> objectList = dbsUtil.dbsFetchListData(sqlStr, devBindInfo, BspsDevBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询语音设备信息失败,instId:<{}>,mchntNo:<{}>.", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (BspsDevBindInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateBompDevBindInfoByMchntStopOrCancel(BspsDevBindInfo devBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompDevBindInfoByMchntStopOrCancel");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, devBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("解绑收银员失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }
}
